begintownscript;
variables;
short choice,i,tick,tick1;
body;

beginstate init_state;
tick = 0;
tick1 = 0;
set_flag(10,6,1);
	if(get_flag(10,4) == 1)
	{ set_terrain(40,15,388);
	set_terrain(32,15,388);
	set_terrain(36,11,388);
	set_terrain(32,18,388);
	set_terrain(40,18,388); }

	if(get_flag(10,5) == 1)
	{	set_terrain(40,15,388);
		set_terrain(32,15,384);
		set_terrain(36,11,388);
		set_terrain(32,18,384);
		set_terrain(40,18,384); }
break;

beginstate exit_state;
break;

beginstate start_state;
	if(get_flag(10,9) == 1) {
		tick = get_current_tick();
		set_flag(10,9,2); }
	if(tick != get_current_tick())
		if(tick != 0)
		{	message_dialog("The beam projectors begin to flicker and spark, they weren't meant to transmit this much energy. The disruption field is growing even stronger, by now it must cover all the tower. You could never cast magic in this field.","A mind numbing shriek fills your head and causes you to stumble. You need to get out of here, the machinery is starting to fail.");
		i = 0;
		while(i < 4)
		{	change_char_energy(i,(-1 * (get_energy(i))));
			set_char_status(i,13,20,1,1);
			i = i + 1; }
		tick = 0;
		tick1 = get_current_tick();
		set_terrain(40,15,388);
		set_terrain(32,15,388);
		set_terrain(36,11,388);
		set_terrain(32,18,388);
		set_terrain(40,18,388);
		}
	if(tick1 != get_current_tick())
		if(tick1 != 0)
		{	message_dialog("The disruption field is out of control. The crystal itself shines brighter than the sun, casting a flickering lurid glow across the walls. The shriek continues, so loud you can hardly think.","Suddenly, an explosion rocks the foundations of the tower, the strain on the machine finally destroyed it. You are knocked to the floor by the force."); 
			set_total_visibility(1);
			force_view_center(36,22);
			put_boom_on_space(36,22,0,0);
			run_animation_sound(152);
			pause(5);

			put_boom_on_space(36,22,1,0);
			put_boom_on_space(35,21,0,0);
			put_boom_on_space(35,22,0,0);
			put_boom_on_space(35,23,0,0);
			put_boom_on_space(36,21,0,0);
			put_boom_on_space(36,22,0,0);
			put_boom_on_space(36,23,0,0);
			put_boom_on_space(37,21,0,0);
			put_boom_on_space(37,22,0,0);
			put_boom_on_space(37,23,0,0);
			run_animation_sound(152);
			pause(5);

			put_boom_on_space(35,21,1,0);
			put_boom_on_space(35,22,1,0);
			put_boom_on_space(35,23,1,0);
			put_boom_on_space(36,21,1,0);
			put_boom_on_space(36,22,1,0);
			put_boom_on_space(36,23,1,0);
			put_boom_on_space(37,21,1,0);
			put_boom_on_space(37,22,1,0);
			put_boom_on_space(37,23,1,0);
			put_boom_on_space(34,20,0,0);
			put_boom_on_space(34,21,0,0);
			put_boom_on_space(34,22,0,0);
			put_boom_on_space(34,23,0,0);
			put_boom_on_space(34,24,0,0);
			put_boom_on_space(35,20,0,0);
			put_boom_on_space(35,24,0,0);
			put_boom_on_space(36,20,0,0);
			put_boom_on_space(36,24,0,0);
			put_boom_on_space(37,20,0,0);
			put_boom_on_space(37,24,0,0);
			put_boom_on_space(38,20,0,0);
			put_boom_on_space(38,21,0,0);
			put_boom_on_space(38,22,0,0);
			put_boom_on_space(38,23,0,0);
			put_boom_on_space(38,24,0,0);
			run_animation_sound(152);
			pause(5);

			put_boom_on_space(34,20,1,0);
			put_boom_on_space(34,21,1,0);
			put_boom_on_space(34,22,1,0);
			put_boom_on_space(34,23,1,0);
			put_boom_on_space(34,24,1,0);
			put_boom_on_space(35,20,1,0);
			put_boom_on_space(35,24,1,0);
			put_boom_on_space(36,20,1,0);
			put_boom_on_space(36,24,1,0);
			put_boom_on_space(37,20,1,0);
			put_boom_on_space(37,24,1,0);
			put_boom_on_space(38,20,1,0);
			put_boom_on_space(38,21,1,0);
			put_boom_on_space(38,22,1,0);
			put_boom_on_space(38,23,1,0);
			put_boom_on_space(38,24,1,0);
			run_animation_sound(152);
			pause(5);

			put_boom_on_space(36,22,0,0);
			run_animation_sound(152);
			set_terrain(36,22,137);
			force_instant_terrain_redraw();
			pause(5);

			put_boom_on_space(36,22,1,0);
			put_boom_on_space(35,21,0,0);
			put_boom_on_space(35,22,0,0);
			set_terrain(35,22,385);
			put_boom_on_space(35,23,0,0);
			set_terrain(35,23,137);
			put_boom_on_space(36,21,0,0);
			put_boom_on_space(36,22,0,0);
			put_boom_on_space(36,23,0,0);
			put_boom_on_space(37,21,0,0);
			put_boom_on_space(37,22,0,0);
			set_terrain(37,22,385);
			put_boom_on_space(37,23,0,0);
			set_terrain(37,23,137);
			run_animation_sound(152);
			force_instant_terrain_redraw();
			pause(5);

			put_boom_on_space(35,21,1,0);
			put_boom_on_space(35,22,1,0);
			put_boom_on_space(35,23,1,0);
			put_boom_on_space(36,21,1,0);
			put_boom_on_space(36,22,1,0);
			put_boom_on_space(36,23,1,0);
			put_boom_on_space(37,21,1,0);
			put_boom_on_space(37,22,1,0);
			put_boom_on_space(37,23,1,0);
			put_boom_on_space(34,20,0,0);
			put_boom_on_space(34,21,0,0);
			set_terrain(34,21,137);
			put_boom_on_space(34,22,0,0);
			set_terrain(34,23,137);
			put_boom_on_space(34,23,0,0);
			set_terrain(34,24,137);
			put_boom_on_space(34,24,0,0);
			put_boom_on_space(35,20,0,0);
			put_boom_on_space(35,24,0,0);
			set_terrain(35,24,137);
			put_boom_on_space(36,20,0,0);
			put_boom_on_space(36,24,0,0);
			put_boom_on_space(37,20,0,0);
			put_boom_on_space(37,24,0,0);
			set_terrain(37,24,137);
			put_boom_on_space(38,20,0,0);
			put_boom_on_space(38,21,0,0);
			set_terrain(38,21,137);
			put_boom_on_space(38,22,0,0);
			put_boom_on_space(38,23,0,0);
			set_terrain(38,23,137);
			put_boom_on_space(38,24,0,0);
			set_terrain(38,24,137);
			run_animation_sound(152);
			force_instant_terrain_redraw();
			pause(5);

			put_boom_on_space(34,20,1,0);
			put_boom_on_space(34,21,1,0);
			put_boom_on_space(34,22,1,0);
			put_boom_on_space(34,23,1,0);
			put_boom_on_space(34,24,1,0);
			put_boom_on_space(35,20,1,0);
			put_boom_on_space(35,24,1,0);
			put_boom_on_space(36,20,1,0);
			put_boom_on_space(36,24,1,0);
			put_boom_on_space(37,20,1,0);
			put_boom_on_space(37,24,1,0);
			put_boom_on_space(38,20,1,0);
			put_boom_on_space(38,21,1,0);
			put_boom_on_space(38,22,1,0);
			put_boom_on_space(38,23,1,0);
			put_boom_on_space(38,24,1,0);
			run_animation_sound(152);
			pause(15);

			i = 0;
			while(char_ok(i) == 0)
			{ i = i + 1; }
			force_view_center(char_loc_x(i),char_loc_y(i));
			put_field_on_space(36,21,6);
			force_instant_terrain_redraw();
			set_total_visibility(0);
			message_dialog("The disruption field blinks out. Hopefully that short exposure to the disruption field was enough to unravel the golems. It certainly was rough on your magic users.","You don't have time to think about it much, you see the thing most feared by adventurers. Quickfire. It is definitely time to get out, and with all haste.");
			tick1 = 0;

}
if(get_flag(10,9) > 0)
{	set_attitude(1001,4);
	set_mobility(1001,0); }	

		
break;

beginstate 10;
if(is_combat() == 0)
	move_to_new_town(4,14,33);
break;

beginstate 11;
	if(get_flag(10,1) == 0)
	{	message_dialog("You look out across the room and get your first glimpse of what is creating the golems. Machinery fills the room below you, there is a machine for making each body part of the golems, and then a final machine to assemble and activate them.","As you watch, this last machine finishes making a golem, and then infuses it with a massive zap of energy. The golem stands, and walks to the end of the room, there is a flash, and it is gone. They must teleport to somewhere else in the tower.");
		set_flag(10,1,1); }
break;

beginstate 12;
	if(get_flag(10,2) == 0)
	{	message_dialog("It looks like this is the communications relay that Tuchin told you to look for. Beams come from a room to the north to power the machinery in here, it looks like it runs on the same source as the golem factory does.","You take out Tuchin's note. The room you are looking at seems to be on a much grander scale than what Tuchin was counting on, there are at least twice as many beams as he expected. Oh well, it should be plenty of energy to power your disruption field.");
		set_flag(10,2,1); }
break;

beginstate 13;
	if(get_flag(10,9) >= 1)
	{	message_dialog("All the controls are jammed, you should make your exit now.","");
		end(); }
	reset_dialog();
	add_dialog_str(0,"You are pleased to see that this control panel has no security devices or traps on it. It seems that Janok wasn't terribly concerned with someone reaching his headquarters.",0);
	add_dialog_str(1,"Theres nothing standing between you and shutting down the machinery. But you remember Tuchin's instuctions and the disruption field. Maybe you could put this to better use by not just destroying it.",0);
	add_dialog_str(2,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Power off/on communications relay.");
	add_dialog_choice(2,"Change power flow.");
	choice = run_dialog(1);
	if(choice == 1)
		end();
	if(choice == 2) {
		if(get_flag(10,3) == 0)
		{	activate_hidden_group(1);
		add_char_to_group(13,1);
		add_char_to_group(16,1);
		add_char_to_group(18,1);
		add_char_to_group(17,1);
		add_char_to_group(19,1);
		add_char_to_group(20,1);
		add_char_to_group(21,1);
		add_char_to_group(22,1);
		add_char_to_group(23,1);
			set_flag(10,3,1); }
		if(get_flag(10,4) == 1)
		{	if(get_flag(10,8) == 1)
			{	message_dialog("The beams fire into the room and refract off the mirrors all the way to the disruption crystal. The now familiar feeling of your magic abilities being stifled returns.","This is a good sign, but the field doesn't feel particularly strong. A lot of power is probably being lost due to the adapted machinery being used. If you want all the golems to feel the effect, you will have to divert more power into the crystal.");
				set_terrain(40,15,384);
				set_terrain(32,15,384);
				set_terrain(36,11,384);
				set_terrain(32,18,384);
				set_terrain(40,18,384);
				set_flag(10,4,0);
				set_flag(10,5,0);
				set_flag(10,6,1);
				end(); }

			message_dialog("The beams fire into the room and refract off the mirrors all the way to the console.","");
			set_terrain(40,15,384);
			set_terrain(32,15,384);
			set_terrain(36,11,384);
			set_terrain(32,18,384);
			set_terrain(40,18,384);
			set_flag(10,4,0);
			set_flag(10,5,0);
			set_flag(10,6,1);
			end(); }	
		if(get_flag(10,4) == 0)
		{	if(get_flag(10,8) == 1)
			{	set_terrain(40,15,388);
				set_terrain(32,15,388);
				set_terrain(36,11,388);
				set_terrain(32,18,388);
				set_terrain(40,18,388);
				message_dialog("The beams flick off, and the disruption field fades away.","");
				set_flag(10,4,1);
				set_flag(10,5,0);
				set_flag(10,6,0);
				end(); }
			set_terrain(40,15,388);
			set_terrain(32,15,388);
			set_terrain(36,11,388);
			set_terrain(32,18,388);
			set_terrain(40,18,388);
			message_dialog("The beams flick off, and an alarm begins to sound. Evidently the room wasn't as trap free as you had hoped. Regardless, you can now get to the console in the middle of the room.","");
			set_flag(10,4,1);
			set_flag(10,5,0);
			set_flag(10,6,0);
			end(); }
}
	if(choice == 3)
	{	if(get_flag(10,4) == 1)
		{	message_dialog("The beams are off. You cannot change the power levels.","");
			end();}
		if(get_flag(10,4) == 0)
	{	reset_dialog();
		add_dialog_str(0,"What would you like to set the power levels to in the communications relay?",0);
		add_dialog_choice(0,"Low power.");
		add_dialog_choice(1,"Regular.");
		add_dialog_choice(2,"Divert all power to relay.");
		choice = run_dialog(1);
		if(choice == 1)
		{	if(get_flag(10,5) == 0)
			{	if(get_flag(10,8) == 1)
				{	message_dialog("The disruption field becomes even weaker, you can hardly feel it.","");
					set_terrain(40,15,388);
					set_terrain(32,15,384);
					set_terrain(36,11,388);
					set_terrain(32,18,384);
					set_terrain(40,18,384);
					set_flag(10,4,0);
					set_flag(10,5,1);
					set_flag(10,6,0);
					end(); }
				message_dialog("The beams dim and the whispers in your head begin to fade, but you still don't think you can get through.","");
				set_terrain(40,15,388);
				set_terrain(32,15,384);
				set_terrain(36,11,388);
				set_terrain(32,18,384);
				set_terrain(40,18,384);
				set_flag(10,4,0);
				set_flag(10,5,1);
				set_flag(10,6,0);
				end(); }
			if(get_flag(10,5) == 1)
				message_dialog("The power is already on low, you see no changes.","");
}
		if(choice == 2)
		{	if(get_flag(10,6) == 1)
			{	message_dialog("The beams are already on regular, nothing changes.","");
				end(); }
			if(get_flag(10,6) == 0)
			{	message_dialog("The beams return to the strength they were at when you first entered.","");
				set_terrain(32,15,384);
				set_terrain(36,11,384);
				set_terrain(32,18,384);
				set_terrain(40,18,384);
				set_flag(10,4,0);
				set_flag(10,5,0);
				set_flag(10,6,1);
				end(); }
}


		if(choice == 3)
		{	if(get_flag(10,8) == 1)
			{	message_dialog("The beams intensify to the point that you have to step further away, the heat is searing. The machinery to the west grinds to a halt, it seems that all the power from the golem factory is being redirected into the disruption crystal.","Even those in your party not gifted in magic are beginning to feel the effects of the disruption field. It drags on your magical equipment and makes you feel lightheaded. You better get out of here.");
				set_flag(10,9,1);
				end(); }
			message_dialog("The lasers begin to power up, but then they fade and return to how they were. A voice booms, _The emergency setting on the communications console has overridden your command._","");
}
}
}
break;

beginstate 14;
if(has_special_item(4) == 0)
	message_dialog("You've already plugged in the disruption crystal, there's nothing left but to turn it on and hope for the best.","");
if(has_special_item(4) == 1) {
	reset_dialog();
	add_dialog_str(0,"The central console lies exposed. It is covered in dials and gauges, but it looks just like Tuchin described; it would be a simple matter to plug in the disruption crystal, and change a few of the dials as Tuchin instructed you.",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Do it.");
	choice = run_dialog(1);
	if(choice == 1)
	{	message_dialog("You step away from the console and leave it how it is.","");
		end(); }
	if(choice == 2)
	{	message_dialog("You remove the communication gem and insert the disruption crystal. Now for the harder part, Tuchin's instructions say to set the emissions dial to maximum, and change the cycle from _burst_ to _static_.","You flip a couple other levers that Tuchin said you needed to. There is, however; one switch Tuchin didn't have in his diagram. It is labeled _emergency override system_, you turn it off. Now you just have to turn on the beams and hope it works.");
		change_spec_item(4,-1);
		set_flag(10,8,1); }
}
break;

beginstate 15;
if(get_flag(10,9) > 1)
	if(num_chars_in_group(1) > 0)
	{	message_dialog("These golems must have been sent here to investigate the alarm you triggered. Now they stand imobilized, drained of the magic that gave them life by the disruption field.","As you watch, they begin to fall apart.");
		kill_char(1001,2,0);
		force_instant_terrain_redraw();
		pause(5);
}
break;

beginstate 16;
	message_dialog("These books detail Janok's attempts to improve golems so that they can survive a disruption field. Fortunately he hasn't figured out how to do it yet, but he seems confident that he soon will be able to.","");
break;

beginstate 17;
	message_dialog("This shelf has some books about the communications relay. They look very similar to the ones from the lab in Smolt, so you don't spend long looking at them.","");
break;

beginstate 19;
	message_dialog("These books contain drawings of new golem models. It took Janok quite a while to advance from the clunky rock golems to the more nimble metal golems. Perhaps his greatest inovation was golems that could cast magic.","Evidently this isn't a natural behavior for golems; golems are held together by magic, but don't actually wield it. You don't completely understand how Janok did it, but evidently they use the magic that holds them together to fuel their spells.");
	set_flag(250,8,1);
break;

